<?php
/**
 *
 * UCModFeed class file
 *
 * @category   UC
 * @author     刘海涛 <liuhaitao01@snda.com>
 * @copyright  1997-2010 SDO Group
 * @version    Release: 1.0.0
 * @link       
 * @license    http://www.sdo.com
 */

require_once(dirname(__FILE__).'/../store/UCModStore.php');
require_once(dirname(__FILE__).'/../../../unified/SDOApiActivitiesPrivilegeService.php');
require_once(dirname(__FILE__).'/../../../unified/SDOApiActivityService.php');
require_once(dirname(__FILE__).'/../../config/UCConfFeed.php');
require_once(dirname(__FILE__).'/../../config/UCConfApps.php');
require_once(dirname(__FILE__).'/../user/UCModUserInfo.php');


class UCModFeed
{
	private	$_error_no ='';
	private $_user_id = '';
	private $_msg = '';
	
	/**
	 * 构造函数
	 *
	 */
	function __construct($user_id)
	{
		if(empty($user_id))
		{
			$this->_error_no = UCConfFeed::FEED_ERROR_PARAM;
			return false;
		}
		$this->_user_id = $user_id;
	}
	
	/**
	 * 析构函数
	 *
	 */
	function __destruct()
	{
	}
	
	/**
	 * 获取错误信息（调试测试用）
	 * @param	int		$ar_feeds	参数同单个动态
	 * @return	bool				true | false
	 *
	 */
	function getErrorMsg()
	{
		$ar_tmp['no'] = $this->_error_no;
		$ar_tmp['msg'] = $this->_msg;
		return $ar_tmp;
	}

	/**
	 * 添加单个动态
	 * @param	array	$ar_feed	发表记录:		array(	'activityType'	=>	'统一动态标识',
															'objectId'		=>	'事务ID',
															'content'		=>	'心情记录描述信息',
															'gameId'		=>	'用户游戏类别的id，游戏端发表的心情记录才有',
															'source'		=>	'心情记录来源: 0 web; 1 游戏;2客户端,3手机等等，留着备用',
															'twitter'		=>	'是否转发',
															'reviewable'	=>	'是否评论',
															'mood'			=>	'心情状态：0一般，1开心，2难过，3愤怒等等',
															'tags'			=>	'心情记录的标签，以逗号隔开',
															'type'			=>	'心情记录类型: 0 文字，1 转推，2 图片，3视频',

														)
									转记录动态:		array(	'activityType'		=>	'统一动态标识',
															'objectId'			=>	'事务ID',
															'content'			=>	'心情记录描述信息',
															'gameId'			=>	'用户游戏类别的id，游戏端发表的心情记录才有',
															'source'			=>	'心情记录来源: 0 web; 1 游戏;2客户端,3手机等等，留着备用',
															'originalContent'	=>	'转发的原贴内容',
															'originalObjectId'	=>	'转发的原始贴mood_id',
															'originalAuthorSdid'=>	'转发的原始贴user_id',
															'twitter'			=>	'是否转发',
															'reviewable'		=>	'是否评论',
															'mood'			=>	'心情状态：0一般，1开心，2难过，3愤怒等等',
															'tags'			=>	'心情记录的标签，以逗号隔开',
															'type'			=>	'心情记录类型: 0 文字，1 转推，2 图片，3视频',
														)
									勋章动态:		array(	'activityType'		=>	'统一动态标识',
															'objectId'			=>	'事务ID',
															'metalId'			=>	'徽章ID',
															'twitter'			=>	'是否转发',
															'reviewable'		=>	'是否评论',
														)
									更新头像动态:	array(	'activityType'		=>	'统一动态标识',
															'linkUrl'			=>	'链接地址',
															'imgUrl'			=>	'图片地址，可空，更新个人头像时需要填入图片链接',
															'reviewable'		=>	'是否评论',
														 )
									添加关注:		array(	'activityType'		=>	'统一动态标识',
															'friendSdid'		=>	'被关注对象ID',
															'inviteType'		=>	'邀请类型：访问主页,推荐, MSN邀请,QQ邀请, Emai邀请,链接邀请',
															'bidrection'		=>	'是否双向关注， defaultValue:false',
															'reviewable'		=>	'是否评论',
														 )
									开通应用:		array(	'activityType'		=>	'UCConfFeed::FEED_TYPE_APP',
															'fromApp'			=>	'产生动态的应用ID',
															'activedAppId'		=>	'开通的应用ID',
															'logoUrl'			=>	'应用的logo链接请',
															'appDesc'			=>	'应用的名称',
															'appUrl'			=>	'应用链接的地址',
															'reviewable'		=>	'是否评论',
														 )
									APP自定义:	array(	'activityType'		=>	'UCConfFeed::FEED_TYPE_APP_SEND',
															'body'			=>	'动态body',
															'extend'		=>	'动态扩展信息',
															'title'			=>	'动态标题',
															'reviewable'		=>	'是否评论',
															'fromApp'		=>	'fromApp',
															'share'		=>	false,
															'utcTime'		=>	utcTime,
														 )

	 * @return	bool				true | false
	 *
	 */
	function addOneFeed($ar_feed, $app_id = false)
	{
		if(!is_array($ar_feed) || count($ar_feed)<1)
		{
			$this->_error_no = UCConfFeed::FEED_ERROR_PARAM;
			return false;
		}

		$ar_feed['sdid'] = $this->_user_id;
		$ar_feed['createTime'] = time();
		if(!array_key_exists('fromApp',$ar_feed))
		{
			$ar_match =		array(	UCConfFeed::FEED_TYPE_AVATAR => UCConfApps::APP_SYSTEM_SET,
									UCConfFeed::FEED_TYPE_POST_TWITTER => UCConfApps::APP_SYSTEM_TWITTER,
									UCConfFeed::FEED_TYPE_CONVEY_TWITTER => UCConfApps::APP_SYSTEM_TWITTER,
									UCConfFeed::FEED_TYPE_IMG_TWITTER => UCConfApps::APP_SYSTEM_TWITTER,
									UCConfFeed::FEED_TYPE_VIDEO_TWITTER => UCConfApps::APP_SYSTEM_TWITTER,
									UCConfFeed::FEED_TYPE_MEDAL => UCConfApps::APP_SYSTEM_MEDAL,
									UCConfFeed::FEED_TYPE_FRIEND => UCConfApps::APP_SYSTEM_FRIEND,
								);
			$ar_feed['fromApp'] = $ar_match[$ar_feed['activityType']];
		}
		//echo json_encode(array($ar_feed));
		
		$ar_tmp = SDOApiActivityService::addActivity(json_encode($ar_feed),'json', $app_id ? $app_id : UCConfFeed::FEED_UC_FLAG);
		if(!$this->_checkReturn($ar_tmp))
		{
			$this->_error_no = UCConfFeed::FEED_ERROR_INTERFACE;
			$this->_msg = $ar_tmp['msg'];
			return false;
		}
		return true;
	}

	/**
	 * 添加自定义动态
	 * @param	int		$ar_feeds	参数同单个动态
	 * @return	bool				true | false
	 *
	 */
	function addTemplateFeed($app_id,$template_id,$ar_title=array(),$ar_body=array())
	{
		if(empty($app_id) || empty($template_id))
		{
			$this->_error_no = UCConfFeed::FEED_ERROR_PARAM;
			return false;
		}
		if(empty($ar_title))
		{
			$title = '{}';
		}
		else
		{
			$title = json_encode($ar_title);
		}

		if(empty($ar_body))
		{
			$body = '{}';
		}
		else
		{
			$body = json_encode($ar_body);
		}

		$ar_tmp = SDOApiActivityService::addTemplatizedActivity($this->_user_id,$title,$body,$template_id,$app_id,time());
		//var_dump($ar_tmp);

		if(!$this->_checkReturn($ar_tmp))
		{
			$this->_error_no = UCConfFeed::FEED_ERROR_INTERFACE;
			$this->_msg = $ar_tmp['msg'];
			return false;
		}
		return true;
	}

	function _parseFeedUsers(&$feed, $key, &$ret1, &$ret2) {
		if (!isset($feed[$key])) return;

		$content = $feed[$key];

		$start = strpos($content, "@{");
		while ($start !== false) {
			$end = $this->_findEndTag($content, $start);
			if ($end < $start || $end === false) break;

			$part = substr($content, $start + 2, $end - $start - 2);
			if (substr($part, 0, 5) == "user:") {
				$sdid = intval(substr($part, 5));
				if ($sdid) {
					$ret1 [$sdid]= 1;
					$ret2 [$sdid]= 1;
				}
			}
			$end += 1;
			$start = strpos($content, "@{", $end);
		}
	}

	function _parseFeedTemplate(&$feed, &$apps, $key) {
		if (!isset($feed[$key])) return "";

		$html = "";
		$end = 0;
		$message = $feed[$key];
		$start = strpos($message, "@{");
		while ($start !== false) {
			if ($start > $end)
				$html .= htmlspecialchars(substr($message, $end, $start - $end));

			$end = $this->_findEndTag($message, $start);
			if ($end < $start || $end === false) break;

			$part = substr($message, $start + 2, $end - $start - 2);

			if ($part == "actor") {
				$sdid = $feed["sdid"];
				if(isset($feed["users"][$sdid])) {
					if (defined("USING_GRAPH")) {
						$html .= "<a target=\"_blank\" href=\"".get_user_home_url($sdid)."\">".$feed['users'][$sdid]['nickname']."</a>";
					} else {
						$html .= "<a href=\"".get_user_home_url($sdid)."\">".$feed['users'][$sdid]['nickname']."</a>";
					}
				}
			} else if ($part == "app") {
				if ($feed["sourceApp"]) {
					if (defined("USING_GRAPH")) {
						$app = $apps[$feed["sourceApp"]["appId"]];
						$url  = "http://t.sdo.com".UCModPanel::getLink($app, true);
						$html .= "<a target=\"_top\" href=\"".$url."\">".$feed["sourceApp"]["name"]."</a>";
					} else {
						$app = $apps[$feed["sourceApp"]["appId"]];
						$html .= "<a ".UCModPanel::getLink($app).">".$feed["sourceApp"]["name"]."</a>";
					}
				}
			} else if (substr($part, 0, 5) == "user:") {
				$sdid = intval(substr($part, 5));
				if(isset($feed["users"][$sdid])) {
					if (defined("USING_GRAPH")) {
						$html .= "<a target=\"_blank\" href=\"".get_user_home_url($sdid)."\">".$feed['users'][$sdid]['nickname']."</a>";
					} else {
						$html .= "<a href=\"".get_user_home_url($sdid)."\">".$feed['users'][$sdid]['nickname']."</a>";
					}
				}
			} else if (substr($part, 0, 4) == "url ") {
				$attributes = $this->_parseAttributes(substr($part, 4));
				$href = isset($attributes["href"]) ? $attributes["href"] : null;
				$name = isset($attributes["name"]) ? $attributes["name"] : null;
				$extend = isset($attributes["extend"]) ? $attributes["extend"] : null;

				if ($href  && $name) {

					if ($extend) {
						$html .= "<a href=\"".$href."\" target=_blank>".$name."</a>";
					} else {
						if ($feed["sourceApp"]) {
							$app = $apps[$feed["sourceApp"]["appId"]];
							$url  = $app["link"];
							$i = strrpos($url, "/");
							if ($i > 7) {
								$url = substr($url, 0, $i);
							}
							if (substr($url, 0, strlen($url) - 1) != "/") $url .= "/";
							if (defined("USING_GRAPH")) {
								$html .= "<a href=\"".$url.$href."\">".$name."</a>";
							} else {
								$html .= "<a href=\"".UCModPanel::getIframeRedirectLink($app, $url.$href)."\">".$name."</a>";
							}
						}
					}
				}
			} else {
				$html .= "@{".$part."}";
			}

			$end += 1;
			$start = strpos($message, "@{", $end);
		}
		$html .= nl2br(htmlspecialchars(substr($message, $end)));
		return $html;
	}

	function _parseAttributes($content) {
		$ret = array();

		$len = strlen($content);
		$i = 0;
		$key = "";
		$value = "";
		$start = 0;
		$status = 0; //0: key开始，1: value开始， 2: value内容开始

		while ($i < $len) {
			$ch = $content[$i];

			switch ($status) {
				case 0:
					if ($ch == "=") {
						$key = substr($content, $start, $i - $start);
						$status = 1;
					}else if ($ch == " " ||  $ch == "\r" || $ch == "\n" || $ch == "\t") {
						$start = $i + 1;
					}
					break;
				case 1:
					if ($ch == "\"") {
						$start = $i + 1;
						$status = 2;
					}
					break;
				case 2:
					if ($ch == "\"" && $content[$i - 1] != "\\") {
						$value = substr($content, $start, $i - $start);
						$ret[trim($key)] = trim(str_replace("\\\"", "\"", $value));
						$status = 0;
						$i += 1;
						$start = $i;
					}
					break;
			}
			$i += 1;
		}

		return $ret;
	}


	function _findEndTag($content, $start) {
		$len = strlen($content);
		$i = $start;
		$cnt = 0;

		while ($i < $len) {
			if ($content[$i] == "}" && $cnt == 0) {
				return $i;
			}

			if ($content[$i] == "\"") {
				if ($i == 0 || $content[$i - 1] != "\\") {
					$cnt = $cnt == 0 ? 1 : 0;
				}
			}
			$i += 1;
		}
		return false;
	}

	/**
	 * 将动态不正确修正为正确的格式
	 */
	function fixFeedFormat(&$data) {
		if(!$data) return;
		if(!is_array($data)) return;
		
		require_once(dirname(__FILE__).'/../store/UCModStore.php');
		require_once(dirname(__FILE__).'/../store/UCModPanel.php');
		
		$user_ids = array();
		$apps = array(); 
		foreach($data as $k => &$item) {
			//APP fix
			$users = array();
			if(isset($item["source"]["appId"])) {
				$source = $item["source"];
				if (isset($source["name"]) && $source["name"] && isset($source["appIcon"]) && $source["appIcon"])  continue;
				$appid = $item["source"]["appId"];
			} elseif($item['source']&&is_int($item['source'])) {
				$appid = $item["source"];
			} elseif(isset($item['fromApp'])) {
				$appid = $item['fromApp'];
			}

			$app = null;

			if($appid) {
				if (isset($apps[$appid])) $app = $apps[$appid];
				else {
					$app = UCModStore::getInstance()->getApp($appid);
					$apps[$appid] = $app;
				}
			}
			if ($app) {
				$src = array();
				$src["appId"] = $appid;
				$src["name"] = $app["title"];
				$src["appIcon"] = UCModPanel::getIcon($appid);
				$src["appIcon32"] = UCModPanel::getIcon($appid, "32");
				$src["appURL"] = UCModPanel::getLink($app);
				$data[$k]["sourceApp"] = $src;
			}
			
			//sdid fix
			if(intval($item['sdid'])==-1){
				if($item['from']['sdid']&&$item['from']['sdid']!=-1) {
					$item['sdid'] = $item['from']['sdid'];
				} elseif(isset($item['sdids'][0])) {
					$item['sdid'] = $item['sdids'][0];
				}
			}

			if(intval($item['sdid']) != -1){
				$user_ids [$item['sdid']] = 1;
				$users[$item['sdid']] = 1;
			}
			
			if(isset($item['originalAuthorSdid'])) {
				$user_ids [$item['originalAuthorSdid']] = 1;
				$users[$item['originalAuthorSdid']] = 1;
			}

			if (isset($item['sdids'])) {
				foreach ($item['sdids'] as $sdid) {
					$user_ids [$sdid] = 1;
					$users[$sdid] = 1;
				}
			}
			
			if (isset($item['friendSdids'])) {
				foreach ($item['friendSdids'] as $sdid) {
					$user_ids[$sdid] = 1;
					$users[$sdid] = 1;
				}
			}
			
			//预定义标签解析，预定义模版
			if($item['activityType'] == UCConfFeed::FEED_TYPE_TEMPLATE) {
				$this->_parseFeedUsers($item, "message", $user_ids, $users);
				$this->_parseFeedUsers($item, "description", $user_ids, $users);
			}

			//预定义标签解析，APP动态
			else if ($item['activityType'] == UCConfFeed::FEED_TYPE_APP_SEND) {
				$this->_parseFeedUsers($item, "title", $user_ids, $users);
				$this->_parseFeedUsers($item, "body", $user_ids, $users);
			}
			$item['users'] = $users;
		}

		$users = array();
		if ($user_ids) {
			$ids = array();
			foreach ($user_ids as $k=>$v) $ids []= $k;
			$users = UCModUserInfo::getUserInfos($ids, array(USERINFO_NICKNAME, USERINFO_AVATAR_30, USERINFO_AVATAR_45, USERINFO_AVATAR_90, USERINFO_AVATAR_170, USERINFO_AUTHED_FLAG));
			foreach($users as $k=>$v) {
				$users[$k] = array(
					"nickname" => $v[USERINFO_NICKNAME],
					"avatar30" => $v[USERINFO_AVATAR_30],
					"avatar45" => $v[USERINFO_AVATAR_45],
					"avatar90" => $v[USERINFO_AVATAR_90],
					"avatar170" => $v[USERINFO_AVATAR_170],
					"offical" => $v[USERINFO_AUTHED_FLAG]
				);
			}
		}

		// 解析标签
		foreach($data as $k => &$item) {
			// 回写users
			foreach ($item["users"] as $sdid=>$v) {
				$item["users"][$sdid] = $users[$sdid];
			}

			//预定义标签解析，预定义模版
			if($item['activityType'] == UCConfFeed::FEED_TYPE_TEMPLATE) {
				$item["titleHTML"] = $this->_parseFeedTemplate($item, $apps, "message");
				$item["bodyHTML"] = $this->_parseFeedTemplate($item, $apps, "description");
			}

			//预定义标签解析，APP动态
			else if($item['activityType'] == UCConfFeed::FEED_TYPE_APP_SEND) {
				$item["titleHTML"] = $this->_parseFeedTemplate($item, $apps, "title");
				$item["bodyHTML"] = $this->_parseFeedTemplate($item, $apps, "body");
			}
		}
	}

	/**
	 * 获取用户某种类型的动态动态信息
	 * @param	int		$timestamp	动态时间戳	（取最新默认为空）
	 * @param	int		$flag		UCConfFeed::FEED_FLAG_SOMEONE：自己的动态 | UCConfFeed::FEED_FLAG_FRIENDS: 好友的动态 | UCConfFeed::FEED_FLAG_ALL
	 * @param	int		$count		条目数
	 * @param	array	$ar_type	动态类型
	 * @param	bool	$previous	false：向后取 | true：向前取
	 * @return	mix				
	 *
	 */
	function getFeedsInfoByUid($timestamp='',$flag=UCConfFeed::FEED_FLAG_ALL,$count=UCConfFeed::FEED_USERHOME_NUM,$ar_type=array(),$previous=true,$ar_app=array(),$except_type=array())
	{
		if(!is_array($ar_type) || !is_bool($previous))
		{
			$this->_error_no = UCConfFeed::FEED_ERROR_PARAM;
			return false;
		}
		if(empty($timestamp))
		{
			$previous = true;
		}
		$ar_param = array('isPrevious'=>$previous,'timestamp'=>$timestamp,'count'=>$count,'range'=>$flag);
		if(!empty($ar_type))
		{
			$ar_param['activityTypes'] = implode(',',$ar_type);
		}

		if(!empty($ar_app))
		{
			$ar_param['acceptedApps'] = implode(',',$ar_app);
		}
		
		if(!empty($except_type))
		{
			$ar_param['excludedTypes'] = implode(',',$except_type);
		}
		//var_Dump($ar_param);
		$ar_feeds = SDOApiActivityService::getFeeds($this->_user_id,$ar_param);   
		if(!$this->_checkReturn($ar_feeds))
		{
			$this->_error_no = UCConfFeed::FEED_ERROR_INTERFACE;
			$this->_msg = $ar_tmp['msg'];                                             
			return false;
		}
        $ar_feeds = $ar_feeds['data']['entry'];                                            
		$this->fixFeedFormat($ar_feeds);
		return $ar_feeds;
	}
	
	/**
	 * 获取一批用户动态信息
	 * @param   array   $ar_uid		用户数组
	 * @param	int		$timestamp	动态时间戳	（取最新默认为空）
	 * @param	int		$count		条目数
	 * @param	array	$ar_type	动态类型
	 * @param	bool	$previous	false：向后取 | true：向前取
	 * @return	mix				
	 *
	 */
	function getUsersFeeds($ar_uid,$timestamp='',$count=UCConfFeed::FEED_USERHOME_NUM,$ar_type=array(),$previous=true, $ar_app=array(),$except_type=array())
	{
		if(!is_array($ar_uid) || !is_array($ar_type) || !is_bool($previous))
		{
			$this->_error_no = UCConfFeed::FEED_ERROR_PARAM;
			return false;
		}
		if(empty($timestamp))
		{
			$previous = true;
		}
		$ar_param = array('timestamp'=>$timestamp,'isPrevious'=>$previous,'count'=>$count);
		if(!empty($ar_type))
		{
			$ar_param['activityTypes'] = implode(',',$ar_type);
		}

		if(!empty($ar_app))
		{
			$ar_param['acceptedApps'] = implode(',',$ar_app);
		}
		
		if(!empty($except_type))
		{
			$ar_param['excludedTypes'] = implode(',',$except_type);
		}
		$ar_feeds = SDOApiActivityService::getUserEvents(implode(',',$ar_uid),$ar_param);
		if(!$this->_checkReturn($ar_feeds))
		{
			$this->_error_no = UCConfFeed::FEED_ERROR_INTERFACE;
			$this->_msg = $ar_tmp['msg'];
			return false;
		}
        $ar_feeds = $ar_feeds['data']['entry'];
		$this->fixFeedFormat($ar_feeds);
		return $ar_feeds;
	}

	/**
	 * 获取用户某种类型的动态动态信息
	 * @param	int		$id			动态ID
	 * @param	array	$ar_param	array('cascade' => '是否级联删除，默认false: true|false')
	 * @return	mix				
	 *
	 */
	public function deleteFeed($id,$ar_param=array())
	{
		if(empty($id))
		{
			$this->_error_no = UCConfFeed::FEED_ERROR_PARAM;
			return false;
		}
		$ar_tmp = SDOApiActivityService::deleteFeeds($this->_user_id, $id, $ar_param);
		//var_dump($ar_tmp);
		if(!$this->_checkReturn($ar_tmp))
		{
			$this->_error_no = UCConfFeed::FEED_ERROR_INTERFACE;
			$this->_msg = $ar_tmp['msg'];
			return false;
		}
		return true;
	}
	
	/**
	 * 判断用户是否在屏蔽列表
	 * @param	string	$uid    指定用户ID
	 * @return  mix				0:不在 | 1：在
	 */
	public function checkUserShield($uid)
	{
		if(empty($uid))
		{
			$this->_error_no = UCConfFeed::FEED_ERROR_PARAM;
			return false;
		}
		$ar_tmp = SDOApiActivitiesPrivilegeService::getJudgeShieldPerson($this->_user_id,$uid);
		if(!$this->_checkReturn($ar_tmp))
		{
			$this->_error_no = UCConfFeed::FEED_ERROR_INTERFACE;
			return false;
		}
		return $ar_tmp['data']['data'];
	}

	/**
	 * 判断应用是否已经在屏蔽应用列表
	 * @param	int	$aid    指定AppId
	 * @return  mix			0:不在 | 1：在
	 */
	public function checkAppShield($aid)
	{
		if(empty($aid))
		{
			$this->_error_no = UCConfFeed::FEED_ERROR_PARAM;
			return false;
		}
		$ar_tmp = SDOApiActivitiesPrivilegeService::getJudgeShieldApp($this->_user_id,$aid);
		if(!$this->_checkReturn($ar_tmp))
		{
			$this->_error_no = UCConfFeed::FEED_ERROR_INTERFACE;
			return false;
		}
		return $ar_tmp['data']['data'];
	}

	/**
	 * 判断是否屏蔽某个应用
	 * @param	int	$aid    指定AppId
	 * @return  mix			0:不在 | 1：在
	 */
	public function checkSelfAppShield($aid)
	{
		if(empty($aid))
		{
			$this->_error_no = UCConfFeed::FEED_ERROR_PARAM;
			return false;
		}
		$ar_tmp = SDOApiActivitiesPrivilegeService::getJudgeSilentApp($this->_user_id,$aid);
		if(!$this->_checkReturn($ar_tmp))
		{
			$this->_error_no = UCConfFeed::FEED_ERROR_INTERFACE;
			return false;
		}
		return $ar_tmp['data']['data'];
	}

	/**
	 * 屏蔽一个指定用户的所有动态
	 * @param	string	$ban_uid    指定用户ID
	 * @return  mix					0:已经添加过 | 1：添加成功 | false：操作失败
	 */
	public function addBanFeedFrend($ban_uid)
	{
		if(empty($ban_uid))
		{
			$this->_error_no = UCConfFeed::FEED_ERROR_PARAM;
			return false;
		}
		$ret = $this->checkUserShield($ban_uid);
		if(false===$ret)
		{
			return false;
		}
		if(!$ret)
		{
			$ar_tmp = SDOApiActivitiesPrivilegeService::addShieldPerson($this->_user_id,$ban_uid);
			if(!$this->_checkReturn($ar_tmp))
			{
				$this->_error_no = UCConfFeed::FEED_ERROR_INTERFACE;
				return false;
			}
			return 1;
		}
		return 0;
	}
	
	/**
	 * 屏蔽自己某个应用的动态
	 * @param	int		$app_id    指定应用ID
	 * @return  bool
	 */
	public function addSelfBanFeedApp($app_id)
	{
		if(empty($app_id))
		{
			$this->_error_no = UCConfFeed::FEED_ERROR_PARAM;
			return false;
		}

		$ret = $this->checkSelfAppShield($app_id);
		if(false===$ret)
		{
			return false;
		}
		if(!$ret)
		{
			$ar_tmp = SDOApiActivitiesPrivilegeService::addSilentApp($this->_user_id,$app_id);
			if(!$this->_checkReturn($ar_tmp))
			{
				$this->_error_no = UCConfFeed::FEED_ERROR_INTERFACE;
				return false;
			}
			return 1;
		}
		return 0;
	}

	/**
	 * 一个指定用户根据AppId 屏蔽一个应用，不接受该应用发来的所有动态
	 * @param	int		$app_id    指定应用ID
	 * @return  bool
	 */
	public function addBanFeedApp($app_id)
	{
		if(empty($app_id))
		{
			$this->_error_no = UCConfFeed::FEED_ERROR_PARAM;
			return false;
		}
			
		$ret = $this->checkAppShield($app_id);
		if(false===$ret)
		{
			return false;
		}
		if(!$ret)
		{
			$ar_tmp = SDOApiActivitiesPrivilegeService::addShieldApp($this->_user_id,$app_id);
			if(!$this->_checkReturn($ar_tmp))
			{
				$this->_error_no = UCConfFeed::FEED_ERROR_INTERFACE;
				return false;
			}
			return 1;
		}
		return 0;
	}

	/**
	 * 获取用户屏蔽的动态好友
	 * @param	string	$ban_uid    指定用户ID
	 * @return  bool
	 */
	public function getBanFeedFrend()
	{
		$ar_tmp = SDOApiActivitiesPrivilegeService::getShieldPeople($this->_user_id);
		if(!$this->_checkReturn($ar_tmp))
		{
			$this->_error_no = UCConfFeed::FEED_ERROR_INTERFACE;
			return false;
		}
		$ar_users = array();
		foreach($ar_tmp['data']['entry'] as $key=>$user)
		{
			$o_userinfo = UCModUserInfo::getUser($user['value']);
			$ar_users[$key]['sdid'] = $user['value'];
			$ar_users[$key]['avatar'] = $o_userinfo->getAvatar(USERINFO_AVATAR_30);
			$ar_users[$key]['nick'] = $o_userinfo->getName();
			$ar_users[$key]['url'] = get_user_home_url($user['value']);
		}
		return $ar_users;
	}


	/**
	 * 获取用户不接受应用的动态应用
	 * @param	int		$app_id    指定应用ID
	 * @return  bool
	 */
	public function getBanFeedApp()
	{
		$ar_tmp = SDOApiActivitiesPrivilegeService::getShieldApp($this->_user_id);
		if(!$this->_checkReturn($ar_tmp))
		{	
			$this->_error_no = UCConfFeed::FEED_ERROR_INTERFACE;
			return false;
		}
		$ar_apps = array();
		foreach($ar_tmp['data']['entry'] as $key=>$app)
		{
			$ar_apps[$app['value']]['id'] = $app['value'];
		}

		return UCModStore::getInstance()->getApps($ar_apps);
	}

	/**
	 * 获取用户不产生动态的应用
	 * @param	int		$app_id    指定应用ID
	 * @return  bool
	 */
	public function getSelfBanFeedApp()
	{
		$ar_tmp = SDOApiActivitiesPrivilegeService::getSilentApp($this->_user_id);
		if(!$this->_checkReturn($ar_tmp))
		{	
			$this->_error_no = UCConfFeed::FEED_ERROR_INTERFACE;
			return false;
		}
		$ar_apps = array();
		foreach($ar_tmp['data']['entry'] as $key=>$app)
		{
			$ar_apps[] = $app['value'];
		}
		//return UCModStore::getApps($ar_apps);
		return $ar_apps;
	}

	/**
	 * 取消用户屏蔽的动态好友
	 * @param	string	$ban_uid    指定用户ID
	 * @return  bool
	 */
	public function delBanFeedFrend($ban_uid)
	{
		if(empty($ban_uid))
		{
			$this->_error_no = UCConfFeed::FEED_ERROR_PARAM;
			return false;
		}
		$ret = $this->checkUserShield($ban_uid);
		if(false===$ret)
		{
			return false;
		}
		if($ret)
		{
			$ar_tmp = SDOApiActivitiesPrivilegeService::deleteShieldPerson($this->_user_id,$ban_uid);
			if(!$this->_checkReturn($ar_tmp))
			{
				$this->_error_no = UCConfFeed::FEED_ERROR_INTERFACE;
				return false;
			}
		}
		return true;
	}


	/**
	 * 取消屏蔽某一个应用
	 * @param	int		$app_id    指定应用ID
	 * @return  bool
	 */
	public function delBanFeedApp($app_id)
	{
		if(empty($app_id))
		{
			$this->_error_no = UCConfFeed::FEED_ERROR_PARAM;
			return false;
		}

		$ret = $this->checkAppShield($app_id);
		if(false===$ret)
		{
			return false;
		}
		if($ret)
		{
			$ar_tmp = SDOApiActivitiesPrivilegeService::deleteShieldApp($this->_user_id,$app_id);
			if(!$this->_checkReturn($ar_tmp))
			{
				$this->_error_no = UCConfFeed::FEED_ERROR_INTERFACE;
				return false;
			}
		}
		return true;
	}

	/**
	 * 取消自己某一个应用不发送动态
	 * @param	int		$app_id    指定应用ID
	 * @return  bool
	 */
	public function delSelfBanFeedApp($app_id)
	{
		if(empty($app_id))
		{
			$this->_error_no = UCConfFeed::FEED_ERROR_PARAM;
			return false;
		}

		$ret = $this->checkSelfAppShield($app_id);
		if(false===$ret)
		{
			return false;
		}
		if($ret)
		{
			$ar_tmp = SDOApiActivitiesPrivilegeService::deleteSilentApp($this->_user_id,$app_id);
			if(!$this->_checkReturn($ar_tmp))
			{
				$this->_error_no = UCConfFeed::FEED_ERROR_INTERFACE;
				return false;
			}
		}
		return true;
	}

	private function _checkReturn($ret)
	{
        return (is_array($ret) && $ret['status']==200 && $ret['code'] == 0)? true:false;
	}
}
?>